package cn.doctorpda.conference.presenter;

import android.os.Handler;

import org.json.JSONException;
import org.json.JSONObject;

import cn.doctorpda.conference.bean.DataInfo;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.config.AppConfig;
import cn.doctorpda.conference.config.NetConfig;
import cn.doctorpda.conference.model.GetRecommHotModel;
import cn.doctorpda.conference.model.IGetModel;
import cn.doctorpda.conference.model.IPostModel;
import cn.doctorpda.conference.okhttp.callback.StringCallback;
import cn.doctorpda.conference.utils.AESUtil;
import cn.doctorpda.conference.utils.GsonUtil;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.view.callbackview.IRecommendView;
import okhttp3.Call;

/**
 * Created by huangjinlong on 2016/5/19.
 */
public class RecommendPresenter {
    private IPostModel model;
    private IRecommendView pageBaseView;

    public RecommendPresenter(IRecommendView baseView) {
        model = new GetRecommHotModel();
        pageBaseView = (IRecommendView) baseView;
    }

    public void getPhysicianResult(final String page, String limit) {
        model.postNetWorkStringData(NetConfig.GET_PHY_RESULT, new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
                pageBaseView.setPhysicianError(call, e);
            }

            @Override
            public void onResponse(String response) {
                try {
                    Msg msg = GsonUtil.fromJson(response, Msg.class);
                    JSONObject jsonObject = new JSONObject(response);
                    DataInfo info = null;
                    if (jsonObject.getString("encrpt_flag").equals("1")) {
                        String data = AESUtil.decrypt(msg.getData(), AppConfig.AES_KEY);
                        info = GsonUtil.fromJson(data, DataInfo.class);
                    } else {
                        info = GsonUtil.fromJson(msg.getData(), DataInfo.class);
                    }
                    if (page.equals("1")){
                        pageBaseView.setRPhysicianResult(msg, info);
                    } else {
                        pageBaseView.setLPhysicianResult(msg, info);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, page, limit);
    }

    public void getRecomHotData(final String page, String limit) {
        model.postNetWorkStringData(NetConfig.GET_HOME_RECOM_HOT, new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
                pageBaseView.setHotError(call, e);
            }

            @Override
            public void onResponse(String response) {
                try {
                    Msg msg = GsonUtil.fromJson(response, Msg.class);
                    JSONObject jsonObject = new JSONObject(response);
                    DataInfo info = null;
                    if (jsonObject.getString("encrpt_flag").equals("1")) {
                        String data = AESUtil.decrypt(msg.getData(), AppConfig.AES_KEY);
                        info = GsonUtil.fromJson(data, DataInfo.class);
                    } else {
                        info = GsonUtil.fromJson(msg.getData(), DataInfo.class);
                    }
                    if (page.equals("1")){
                        pageBaseView.setRHotResult(msg, info);
                    } else {
                        pageBaseView.setLHotResult(msg, info);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, page, limit, "hot");
    }

    public void getLocalResult(final String page, String limit, String province){
        model.postNetWorkStringData(NetConfig.GET_HOME_RECOM_HOT, new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
                pageBaseView.setLocalError(call, e);
            }

            @Override
            public void onResponse(String response) {
                try {
                    Msg msg = GsonUtil.fromJson(response, Msg.class);
                    JSONObject jsonObject = new JSONObject(response);
                    DataInfo info = null;
                    if (jsonObject.getString("encrpt_flag").equals("1")) {
                        String data = AESUtil.decrypt(msg.getData(), AppConfig.AES_KEY);
                        info = GsonUtil.fromJson(data, DataInfo.class);
                        L.d("山东"+info.getPAGE().getList().get(0).getCLIENT_BANNER());
                    } else {
                        info = GsonUtil.fromJson(msg.getData(), DataInfo.class);
                    }
                    if (page.equals("1")){
                        pageBaseView.setRLocalResult(msg, info);
                        L.d("山东"+info.getPAGE().getList().get(0).getCLIENT_BANNER());
                    } else {
                        pageBaseView.setLLocalResult(msg, info);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, page, limit, province);
    }

//    public void getInfoResult(String page, String limit) {
//        model.getNetWorkStringData(NetConfig.GET_DOC, new StringCallback() {
//            @Override
//            public void onError(Call call, Exception e) {
//                pageBaseView.setHotError(call, e);
//            }
//
//            @Override
//            public void onResponse(String response) {
//                try {
//                    Msg msg = GsonUtil.fromJson(response, Msg.class);
//                    JSONObject jsonObject = new JSONObject(response);
//                    DataInfo info = null;
//                    if (jsonObject.getString("encrpt_flag").equals("1")) {
//                        String data = AESUtil.decrypt(msg.getData(), AppConfig.AES_KEY);
//                        info = GsonUtil.fromJson(data, DataInfo.class);
//                    } else {
//                        info = GsonUtil.fromJson(msg.getData(), DataInfo.class);
//                    }
//                    pageBaseView.setHotResult(msg, info);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        }, page, limit, "hot");
//    }

}
